package com.edu.edusystem.service.impl;

import com.edu.edusystem.domain.*;
import com.edu.edusystem.mapper.RoleMapper;
import com.edu.edusystem.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<Role> findAllRole(Role role) {
        List<Role> allRole = roleMapper.findAllRole(role);
        return allRole;
    }

    @Override
    public List<Integer> findMenuByRoleId(Integer roleid) {
        List<Integer> menuByRoleId = roleMapper.findMenuByRoleId(roleid);

        return menuByRoleId;
    }

    @Override
    public void saveRole(Role role) {
        Date date = new Date();
        role.setCreatedTime(date);
        role.setUpdatedTime(date);
        role.setCreatedBy("tcbn");
        role.setUpdatedBy("tcbn");

        roleMapper.saveRole(role);
    }

    @Override
    public void updateRole(Role role) {
        Date date = new Date();
        role.setUpdatedTime(date);
        role.setUpdatedBy("tcbn");

        roleMapper.update(role);
    }
    @Override
    public void roleContextMenu(RoleMenuVo roleMenuVo) {

        //1. 清空中间表的关联关系
        roleMapper.deleteRoleContextMenu(roleMenuVo.getRoleId());

        //2. 为角色分配菜单

        for (Integer mid : roleMenuVo.getMenuIdList()) {

            Role_menu_relation role_menu_relation = new Role_menu_relation();
            role_menu_relation.setMenuId(mid);
            role_menu_relation.setRoleId(roleMenuVo.getRoleId());

            //封装数据
            Date date = new Date();
            role_menu_relation.setCreatedTime(date);
            role_menu_relation.setUpdatedTime(date);
            role_menu_relation.setCreatedBy("tcbn");
            role_menu_relation.setUpdatedby("tcbn");

            roleMapper.roleContextMenu(role_menu_relation);
        }

    }

    @Override
    public void deleteRole(Integer roleid) {

        // 调用根据roleid清空中间表关联关系
        roleMapper.deleteRoleContextMenu(roleid);
        //根据角色id 清空对应的中间表
        roleMapper.deleteRoleContextResource(roleid);

        roleMapper.deleteRole(roleid);
    }

    /*
        获取当前角色拥有的所有资源信息 资源分类 + 资源信息
    */
    @Override
    public List<ResourceCategory> findResourceListByRoleId(int id) {

        //获取角色拥有的资源分类信息
        List<ResourceCategory> categoryList = roleMapper.findResourceCategoryByRoleId(id);

        //获取角色拥有的资源信息
        List<Resource> resourceList = roleMapper.findResourceByRoleId(id);

        System.out.println(resourceList);
        //将资源信息封装到对应资源分类分类下
        for (ResourceCategory category : categoryList) {
            for (Resource resource : resourceList) {

                if (category.getId() == resource.getCategoryId()){
                    //将资源保存到集合中
                    category.getResourceList().add(resource);
                }
            }
        }
        return categoryList;

    }

    /*
         为角色分配资源   角色添加& 修改资源
    */
    @Override
    public void roleContextResource(RoleResourceVo roleResourceVo) {

        //根据角色id 清空对应的中间表
        Integer roleId = roleResourceVo.getRoleId();
        roleMapper.deleteRoleContextResource(roleId);

        //获取资源的id集合
        List<Integer> resourceIdList = roleResourceVo.getResourceIdList();

        //向中间表插入最新的关联信息
        for (Integer ResourceId : resourceIdList) {
            RoleResourceRelation roleResourceRelation = new RoleResourceRelation();

            roleResourceRelation.setRoleId(roleId);
            roleResourceRelation.setResourceId(ResourceId);

            Date date = new Date();
            roleResourceRelation.setCreatedTime(date);
            roleResourceRelation.setUpdatedTime(date);
            roleResourceRelation.setCreatedBy("system");
            roleResourceRelation.setUpdatedBy("system");

            roleMapper.roleContextResource(roleResourceRelation);

        }

    }




}
